/*
 * Copyright 2007-208 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ 
package com.cassevern.storproc.util
import java.awt.*
import javax.swing.table.*
import javax.swing.*  

swing = new groovy.swing.SwingBuilder()

build = swing.action(
  name:'Build',
  closure:this.&buildCallBeans, 
  mnemonic:'R'   
)

def errorPanel = {
	swing.panel(constraints: BorderLayout.NORTH) {
		textField(id:'error', columns:50, editable:false, '') 
	}
}
def buttonPanel = {
	swing.panel(constraints: BorderLayout.SOUTH) {
	    button(text:'Build', action:build)
	}
}
def inputPanel = {
	swing.panel(constraints: BorderLayout.CENTER) {
		panel(layout: new GridLayout(3, 2, 5, 5)) {
		    label('IP Address:', horizontalAlignment: JLabel.RIGHT)
		    textField(id:'ip', columns:15, '')
		    label('User:', horizontalAlignment: JLabel.RIGHT)
		    textField(id:'user', columns:10, '')
		    label('Password:', horizontalAlignment: JLabel.RIGHT)
		    passwordField(id:'pass', columns:10, '')
		    label('Library:', horizontalAlignment: JLabel.RIGHT)
		    textField(id:'lib', columns:10, '')
		    label('Stored Procedure:', horizontalAlignment: JLabel.RIGHT)
		    textField(id:'procName', columns:15, '')
		    label('Handle SQL Result Set:', horizontalAlignment: JLabel.RIGHT)
		    checkBox(id:'resultSet', selected:true)
		}
	} // close swing.panel
} // close inputPanel closure

def mainPanel = {
	swing.panel(layout: new BorderLayout()) {
		errorPanel()
		inputPanel()
		buttonPanel()
	} 
} // close mainPanel closure
frame = swing.frame(title:'Build Java Beans From a Stored Procedure') {
	mainPanel()
}

frame.pack()
frame.show()

def buildCallBeans(event) {
	swing.error.text = ''
	def builder = new BuildCallBeansFromStorProc(
					ip:swing.ip.text, 
					user:swing.user.text, 
					password:swing.pass.text, 
					library:swing.lib.text, 
					procName:swing.procName.text, 
					handleResultSet:swing.resultSet.selected)
					//pcmlDirectory:swing.fileChooser.selectedFile.path
					//pcmlDirectory:swing.pcmlDir.text
	try {
		builder.run()
		frame.setVisible(false)
	} catch (Exception e) {
		swing.error.text = e.toString()
		e.printStackTrace()
	}
}